const connection = require("../service/file.service")
const userConnection = require("../service/user.service")
const {APP_HOST,APP_PORT} = require("../app/config")

class FileController {
 async saveAvatarInfo(ctx,next){
    //1.获取图像相关的信息
    const { filename,mimetype,size} = ctx.req.file;
    const {id} = ctx.user
    //2.将图像信息保存到数据库中
    let result = await connection.uploadAvatar( filename,mimetype,size,id)
    //3.将头像路径存到我们的user表中
    const avatarUrl = `${APP_HOST}:${APP_PORT}/users/${id}/avatar`
    await userConnection.updateAvatarUrlById(id,avatarUrl)
    ctx.body = '用户上传头像成功'
  }

  async savePictureInfo(ctx,next){
    // 1.获取文件信息
    const files = ctx.req.files;
    const {id:userId} = ctx.user
    const {momentId} = ctx.query
    for (const file of files) {
      const {filename,mimetype,size} = file
      await connection.createPicture(filename,mimetype,size,userId,momentId)
    }
    
    ctx.body = "动态配图上传完成"

  }
}

module.exports = new FileController()